package cn.s1995.sloth.bms.controller;

import java.util.List;
import java.util.Objects;

import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.s1995.sloth.bizs.entity.Video;
import cn.s1995.sloth.bizs.service.VideoService;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.excel.annotation.ResponseExcel;
import cn.s1995.sloth.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

/**
 * 视频
 *
 * @author Sasshi
 * @date 2023-12-12 10:51:22
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/video")
@Tag(description = "Video", name = "BMS-视频管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class VideoController {

    private final VideoService videoService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param req 视频
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('bizs_Video_view')")
    public R getVideoPage(@ParameterObject Page page, @ParameterObject Video req) {
    	LambdaQueryWrapper<Video> wrapper = buildQueryWrapper(req);
        return R.ok(videoService.page(page, wrapper));
    }


    /**
     * 通过id查询视频
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('bizs_Video_view')")
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(videoService.getVideoAndRecommend(id));
    }

    /**
     * 新增视频
     * @param req 视频
     * @return R
     */
    @Operation(summary = "新增视频", description = "新增视频")
    @SysLog("新增视频")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('bizs_Video_add')")
    public R save(@RequestBody Video req) {
        return R.ok(videoService.saveVideoAndRecommend(req));
    }

    /**
     * 修改视频
     * @param req 视频
     * @return R
     */
    @Operation(summary = "修改视频", description = "修改视频")
    @SysLog("修改视频")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('bizs_Video_edit')")
    public R updateById(@RequestBody Video req) {
    	return R.ok(videoService.saveVideoAndRecommend(req));
    }

    /**
     * 通过id删除视频
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除视频", description = "通过id删除视频")
    @SysLog("通过id删除视频")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('bizs_Video_del')")
    public R removeById(@RequestBody Long[] ids) {
    	for (Long id : ids) {
    		videoService.syncCacheToDB(id);
			videoService.removeCache(id);
		}
        return R.ok(videoService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param req 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @Operation(summary = "导出excel表格", description = "")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('bizs_Video_export')")
    public List<Video> export(Video req, Long[] ids) {
		LambdaQueryWrapper<Video> wrapper = buildQueryWrapper(req);
		wrapper.in(ArrayUtil.isNotEmpty(ids), Video::getId, ids);
        return videoService.list(wrapper);
    }

	private LambdaQueryWrapper<Video> buildQueryWrapper(Video req) {
        LambdaQueryWrapper<Video> wrapper = Wrappers.lambdaQuery();
        wrapper.and(
                StrUtil.isNotEmpty(req.getSearch()),
                w -> w.like(Video::getName, req.getSearch())
                        .or()
                        .like(Video::getDesc, req.getSearch())
        );
		wrapper.like(StrUtil.isNotBlank(req.getSearch()), Video::getDesc, req.getSearch());
		wrapper.eq(Objects.nonNull(req.getState()), Video::getState, req.getState());
		wrapper.eq(Objects.nonNull(req.getIsSale()), Video::getIsSale, req.getIsSale());
		wrapper.orderByDesc(Video::getCreateTime);
		return wrapper;
	}
}
